package com.ruoyi.chat.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.chat.domain.Friendship;
import com.ruoyi.chat.service.IFriendshipService;
import com.ruoyi.common.core.cond.AddFriendRequestCond;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.vo.AddFriendUserVO;
import com.ruoyi.chat.domain.ChatMessage;
import com.ruoyi.chat.service.IChatMessageService;
import com.ruoyi.chat.service.IFriendRequestService;
import com.ruoyi.common.core.vo.ChatListVO;
import com.ruoyi.common.core.vo.FriendVO;
import com.ruoyi.common.core.web.page.PageDomain;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 聊天信息存储Controller
 * 
 * @author zhc
 * @date 2025-05-31
 */
@RestController
@RequestMapping("/chat")
public class ChatMessageController extends BaseController
{
    @Autowired
    private IChatMessageService chatMessageService;
    @Resource
    private IFriendRequestService iFriendRequestService;

    @Resource
    private IFriendshipService iFriendshipService;
    @Resource
    private IChatMessageService iChatMessageService;
    /**
     * 查询聊天信息存储列表
     */
    @RequiresPermissions("chat:message:list")
    @GetMapping("/list")
    public TableDataInfo list(ChatMessage chatMessage)
    {
        startPage();
        List<ChatMessage> list = chatMessageService.selectChatMessageList(chatMessage);
        return getDataTable(list);
    }

    /**
     * 导出聊天信息存储列表
     */
    @RequiresPermissions("chat:message:export")
    @Log(title = "聊天信息存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatMessage chatMessage)
    {
        List<ChatMessage> list = chatMessageService.selectChatMessageList(chatMessage);
        ExcelUtil<ChatMessage> util = new ExcelUtil<ChatMessage>(ChatMessage.class);
        util.exportExcel(response, list, "聊天信息存储数据");
    }

    /**
     * 获取聊天信息存储详细信息
     */
    @RequiresPermissions("chat:message:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return new AjaxResult().success(chatMessageService.selectChatMessageById(id));
    }

    /**
     * 新增聊天信息存储
     */
    @RequiresPermissions("chat:message:add")
    @Log(title = "聊天信息存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatMessage chatMessage)
    {
        return toAjax(chatMessageService.insertChatMessage(chatMessage));
    }

    /**
     * 修改聊天信息存储
     */
    @RequiresPermissions("chat:message:edit")
    @Log(title = "聊天信息存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatMessage chatMessage)
    {
        return toAjax(chatMessageService.updateChatMessage(chatMessage));
    }

    /**
     * 删除聊天信息存储
     */
    @RequiresPermissions("chat:message:remove")
    @Log(title = "聊天信息存储", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(chatMessageService.deleteChatMessageByIds(ids));
    }

    /**
     * 添加好友查询
     * @param cond
     * @return
     */
    @GetMapping("/add-friend-query")
    public AjaxResult<AddFriendUserVO> addFriendQuery(@RequestParam("cond")String cond){
        return new AjaxResult<AddFriendUserVO>().success(iFriendRequestService.addFriendQuery(cond));
    }

    /**
     * 好友申请
     * @param addFriendRequestCond
     * @return
     */
    @PostMapping("/add-friend-request")
    public AjaxResult<Boolean> addFriendRequest(@RequestBody AddFriendRequestCond addFriendRequestCond){
        return new AjaxResult<Boolean>().success(iFriendRequestService.addFriendRequest(addFriendRequestCond));
    }

    /**
     * 查询好友申请
     * @return
     */
    @GetMapping("/query-friend-request")
    public AjaxResult<List<AddFriendUserVO>> queryFriendRequest(){
        return new AjaxResult<List<AddFriendUserVO>>().success(iFriendRequestService.queryFriendRequest());
    }

    /**
     * 处理好友申请
     * @param id
     * @param executeType
     * @return
     */
    @GetMapping("/execute-friend-request")
    public AjaxResult<Boolean> executeFriendRequest(@RequestParam("id")Long id,@RequestParam("executeType")String executeType){
        return new AjaxResult<Boolean>().success(iFriendRequestService.executeFriend(id,executeType));
    }

    /**
     * 查询通讯录
     * @return
     */
    @GetMapping("/queryFriend")
    public AjaxResult<List<Friendship>> queryFriend(){
        return new AjaxResult<List<Friendship>>().success(iFriendshipService.queryFriend());
    }

    /**
     * 查询最近的聊天
     */
    @GetMapping("/query-chat-list")
    public AjaxResult<List<ChatListVO>> queryChatList(){
        return new AjaxResult<List<ChatListVO>>().success(iChatMessageService.queryChatList());
    }
    @GetMapping("/query-chat-history/{userId}")
    public AjaxResult<TableDataInfo> queryChatHistory(@PathVariable("userId")String userId){
        PageUtils.startPage();
        TableDataInfo dataTable = getDataTable(iChatMessageService.queryChatHistory(userId));
        return new AjaxResult<TableDataInfo>().success(dataTable);
    }
}
